<?php
/**
 * Created by 784855684@qq.com.
 * Link: https://github.com/lichtung/wshore
 * User: linzh
 * Date: 7/7/17
 * Time: 8:18 AM
 */
declare(strict_types=1);


namespace application\module\admin\addon\property\model;


use application\module\admin\addon\model\AddonModel;
use wshore\throws\db\RecordNotFoundException;

/**
 * Class PropertyModel
 *
 * @property string $name
 * @property string $value
 * @property string $note
 *
 *
 * @package application\module\admin\addon\property\model
 */
class PropertyModel extends AddonModel
{

    protected function struct(): string
    {
        return "  `id` int(10) unsigned NOT NULL AUTO_INCREMENT,
  `name` varchar(255) CHARACTER SET ascii NOT NULL COMMENT '配置名称',
  `value` varchar(255) CHARACTER SET utf8 NOT NULL DEFAULT '' COMMENT '配置值',
  `note` varchar(255) COLLATE utf8_unicode_ci NOT NULL DEFAULT '' COMMENT '备注',
  `update_time` timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP COMMENT '修改时间',
  PRIMARY KEY (`id`)";
    }


    protected function tableName()
    {
        return 'property';
    }


    public function __construct($id = null)
    {
        if ($id) {
            if (is_numeric($id)) {
                parent::__construct($id);
            } else {
                parent::__construct();
                $data = $this->where(['name' => $id])->find();
                if (!$data) {
                    throw new RecordNotFoundException($id);
                }
                $this->data = $data;
                $this->id = $this->data[$this->pk];
            }
        } else {
            parent::__construct();
        }
    }
}